package com.ruoyi.web.controller.shopping;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.shopping.domain.MallProduct;
import com.ruoyi.shopping.service.IMallProductService;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.shopping.domain.MallClassify;
import com.ruoyi.shopping.service.IMallClassifyService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 商城分类Controller
 *
 * @author scx
 * @date 2022-06-10
 */
@RestController
@RequestMapping("/shopping/classify")
public class MallClassifyController extends BaseController
{
    @Autowired
    private IMallClassifyService mallClassifyService;
    @Autowired
    private IMallProductService mallProductService;

    /**
     * 查询一级分类
     * */
    @GetMapping("/firstGrade")
    public AjaxResult firstGrade(MallClassify mallClassify) {
        //获取一级分类
        mallClassify.setParentId(0L);
        List<MallClassify> mallClassifies = mallClassifyService.selectMallClassifyList(mallClassify);
        return AjaxResult.success(mallClassifies);
    }
    /**
     * 查询二级分类
     * */
    @GetMapping("/secondGrade")
    public TableDataInfo secondGrade(Long id) {
        //获取二级分类
        MallClassify mallClassify = new MallClassify();
        mallClassify.setParentId(id);
        List<MallClassify> mallClassifies = mallClassifyService.selectMallClassifyList(mallClassify);
        return getDataTable(mallClassifies);
    }
    /**
    * 商城商品里的分类所需要数据
    * */
    @GetMapping("/treequery")
    public TableDataInfo liststate(Long id) {
        //获取一级分类和二级分类
        List<MallClassify> list = new ArrayList<MallClassify>();
        if (id == null) {
            MallClassify mallClassify = new MallClassify();
            mallClassify.setParentId(0L);
            list = mallClassifyService.selectMallClassifyList(mallClassify);
        }
        if (id !=null){
            MallClassify mallClassify1 = new MallClassify();
            mallClassify1.setParentId(id);
            list = mallClassifyService.selectMallClassifyList(mallClassify1);
        }
        for (MallClassify mallClassify : list) {
            MallClassify  MallClassQuery = new MallClassify();
            MallClassQuery.setParentId(mallClassify.getId());
            List<MallClassify> listQuery = mallClassifyService.selectMallClassifyList(MallClassQuery);
            if(listQuery.size()>0){
                mallClassify.setLeaf(false);
            }else{
                mallClassify.setLeaf(true);
            }
        }

        return getDataTable(list);
    }



    /**
     * 查询商城分类列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:classify:list')")
    @GetMapping("/list")
    public AjaxResult list(MallClassify mallClassify)
    {
        List<MallClassify> list = mallClassifyService.selectMallClassifyList(mallClassify);
        return AjaxResult.success(list);

    }

    /**
     * 导出商城分类列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:classify:export')")
    @Log(title = "商城分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallClassify mallClassify)
    {
        List<MallClassify> list = mallClassifyService.selectMallClassifyList(mallClassify);
        ExcelUtil<MallClassify> util = new ExcelUtil<MallClassify>(MallClassify.class);
        return util.exportExcel(list, "商城分类数据");
    }

    /**
     * 获取商城分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:classify:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallClassifyService.selectMallClassifyById(id));
    }

    /**
     * 新增商城分类
     */
    @PreAuthorize("@ss.hasPermi('shopping:classify:add')")
    @Log(title = "商城分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallClassify mallClassify)
    {
        MallClassify query = new MallClassify();
        query.setParentId(mallClassify.getParentId());
        query.setClassifyName(mallClassify.getClassifyName());
        query.setStatus("Y");
        query.getParams().put("classifyNameEQ",mallClassify.getClassifyImg());
        List<MallClassify> classifies = mallClassifyService.selectMallClassifyList(query);
        if (classifies.size() > 0) {
            return AjaxResult.error("该名称已存在,请勿重复添加");
        }
        return toAjax(mallClassifyService.insertMallClassify(mallClassify));
    }

    public static void main(String[] args) {
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(1);
        if(integers.size()>1){
            integers.clear();
        }
    }

    /**
     * 修改商城分类
     */
    @PreAuthorize("@ss.hasPermi('shopping:classify:edit')")
    @Log(title = "商城分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallClassify mallClassify)
    {
        mallClassify.setUpdateType(DicConstants.BUSINESS_Manager.getCode());

        return toAjax(mallClassifyService.updateMallClassify(mallClassify));
    }

    /**
     * 删除商城分类
     */
    @PreAuthorize("@ss.hasPermi('shopping:classify:remove')")
    @Log(title = "商城分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            MallClassify mallClassify = new MallClassify();
            mallClassify.setParentId(id);
            List list = mallClassifyService.selectMallClassifyList(mallClassify);
            if (list.size() > 0) {
                return AjaxResult.error("请先删除子节点");
            }
        }
        int res = mallClassifyService.logicDeleteMallClassify(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId());
        return toAjax(res);
    }
}
